package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.repository.ValEngineeringRepository;
import com.css.fxfzysx.modules.ysxManagement.entity.*;
import com.css.fxfzysx.modules.ysxManagement.repository.CecMainRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.CecMainService;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.BdFactorVo;
import com.css.fxfzysx.modules.ysxManagement.vo.CnRatioVo;
import com.css.fxfzysx.modules.ysxManagement.vo.SrBaselineVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CecMainServiceImpl implements CecMainService {

    @Autowired
    private CecMainRepository cecMainRepository;

    @Autowired
    private ZqtCiService zqtCiService;

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Autowired
    private ValEngineeringRepository valEngineeringRepository;

    @Override
    public RestResponse saveOrUpdateCnRatio(CnRatioVo cnRatioVo) {
        try {
            List<CnRatioEntity> cnRatioList = cnRatioVo.getCnRatioList();
            List<DynamicFieldsEntity> dynamicFieldsList = cnRatioVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(cnRatioList)) {
                cnRatioList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCcrId())){
                        it.setCcrId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        cecMainRepository.saveCnRatio(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        List<CnRatioEntity> list = cecMainRepository.getCnRatioById(it.getCcrId());
                        list.forEach(cn->{
                            if (!cn.getCategoryValue().equals(it.getCategoryValue())){
                                valEngineeringRepository.updateCountStatusByModelIds1Id(it.getCcrId());
                            }
                        });
                        cecMainRepository.updateCnRatio(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllCnRatio(String titleName) {
        try {
            CnRatioVo cnRatioVo = new CnRatioVo();
            List<CnRatioEntity> cnRatioList = cecMainRepository.getAllCnRatio();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            cnRatioVo.setCnRatioList(cnRatioList);
            cnRatioVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(cnRatioVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteCnRatioById(String cnRatioIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(cnRatioIds)){
                for (String cnRatioId : cnRatioIds.split(",")) {
                    cecMainRepository.deleteCnRatioById(cnRatioId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    cecMainRepository.deleteCnRatioByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse saveOrUpdateSrBaseline(SrBaselineVo srBaselineVo) {
        try {
            List<SrBaselineEntity> srBaselineList = srBaselineVo.getSrBaselineList();
            List<DynamicFieldsEntity> dynamicFieldsList = srBaselineVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(srBaselineList)) {
                srBaselineList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCsrbId())){
                        it.setCsrbId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        cecMainRepository.saveSrBaseline(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        List<SrBaselineEntity> list = cecMainRepository.getSrById(it.getCsrbId());
                        list.forEach(sr->{
                            if (!sr.getSiteValue().equals(it.getSiteValue())){
                                valEngineeringRepository.updateCountStatusByModelIds1Id(it.getCsrbId());
                            }
                        });
                        cecMainRepository.updateSrBaseline(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllSrBaseline(String titleName) {
        try {
            SrBaselineVo srBaselineVo = new SrBaselineVo();
            List<SrBaselineEntity> srBaselineList = cecMainRepository.getAllSrBaseline();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            srBaselineVo.setSrBaselineList(srBaselineList);
            srBaselineVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(srBaselineVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteSrBaselineById(String srBaselineIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(srBaselineIds)){
                for (String srBaselineId : srBaselineIds.split(",")) {
                     cecMainRepository.deleteSrBaselineById(srBaselineId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    cecMainRepository.deleteSrBaselineByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
